/****************************************************************
 ** Product  :   HP Subscriber Network Application Policy
 ** Module   :   gr-manager
 ** Date: Apr 10, 2015               
 ** Author: Hua Zhen
 ** (C) Copyright 2015, Hewlett-Packard Company, All Rights Reserved.
 ** This software is the proprietary information of HP, Inc.  
 ** Use is subject to license terms.
 *****************************************************************
 ** Description:   
 **    Merge DB status info and show in console with YAML format.
 **    
 ****************************************************************/
package com.hp.snap.gr.action.impl;

import java.io.LineNumberReader;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;

import com.hp.snap.gr.exception.ActionException;
import com.hp.snap.gr.model.DBLocalStructuredInfo;
import com.hp.snap.gr.model.DBRemoteStructuredInfo;
import com.hp.snap.gr.model.DBStatusInfo;
import com.hp.snap.gr.model.SPRStatusInfo;
import com.hp.snap.gr.model.SPRStatusInfos;
import com.hp.snap.gr.model.jaxb.ActionType;
import com.hp.snap.gr.utils.Constants;
import com.hp.snap.gr.utils.Utils;

/**
 * 
 * Merge DB status info from action output and show in console with YAML format. 
 * 
 * There is no parameter can be configured in the action.
 * From the action output get related DB status info.
 *    LocalDBStatusInfo: local DB status info.
 *    RemoteDBStatusInfo: remote DB status info.
 * 
 * <P>
 * Version: 3.3 <br>
 * Author: Hua Zhen
 * Date: Apr 10, 2015
 * 
 * </P>
 * 
 **/
public class MergeSPRStatusInfoActionHandler extends AbstractActionHandler  {
	private static Logger logger = LoggerFactory.getLogger(MergeSPRStatusInfoActionHandler.class);

	@Override
	public void execute(ActionType action,
			Map<String, Object> actionPlanInputs,
			Map<String, Object> actionPlanOutputs) throws ActionException {
		SPRStatusInfo statusInfo = new SPRStatusInfo();
		DBLocalStructuredInfo localDBInfo = new DBLocalStructuredInfo();
		DBRemoteStructuredInfo remoteDBInfo = new DBRemoteStructuredInfo();
		
		//set deployment name
		localDBInfo.setLocal_1_deploymentName(Utils.getConfigurationValue(Constants.CFG_GRL_DEPLOY_NAME));
        remoteDBInfo.setRemote_1_deploymentName(Utils.getCorrespondSiteName(localDBInfo.getLocal_1_deploymentName()));
        
        if (actionPlanOutputs.get(Constants.DB_STATUS_INFO_LOCAL) != null) {
            Map<String, DBStatusInfo> dbStatusMap = (Map<String, DBStatusInfo>) actionPlanOutputs.get(Constants.DB_STATUS_INFO_LOCAL);
            List<DBStatusInfo> dbStatusLst = new ArrayList<DBStatusInfo>();
            for (String key : dbStatusMap.keySet()) {
                DBStatusInfo obj = dbStatusMap.get(key);
                dbStatusLst.add(obj);
            }
            localDBInfo.setLocal_4_SgsStatus(dbStatusLst);
        }
        
        if (actionPlanOutputs.get(Constants.DB_STATUS_INFO_REMOTE) != null) {
            Map<String, DBStatusInfo> dbStatusMap = (Map<String, DBStatusInfo>) actionPlanOutputs.get(Constants.DB_STATUS_INFO_REMOTE);

            List<DBStatusInfo> dbStatusLst = new ArrayList<DBStatusInfo>();
            for (String key : dbStatusMap.keySet()) {
                DBStatusInfo obj = dbStatusMap.get(key);
                dbStatusLst.add(obj);
            }
            remoteDBInfo.setRemote_4_SgsStatus(dbStatusLst);
        }

        try {
            statusInfo.setLocal_deployment(localDBInfo);
            statusInfo.setRemote_deployment(remoteDBInfo);
            SPRStatusInfos statusInfos = new SPRStatusInfos();
            statusInfos.getStatusInfos().add(statusInfo);
            Map<String, String> statusInfoLabel = Utils.getStatusInfoLabelMap();
            DumperOptions options = new DumperOptions();
            options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
            Yaml yaml = new Yaml(options);
            String x = yaml.dump(statusInfos);
            x = Utils.replaceString(x, statusInfoLabel);
            LineNumberReader r = new LineNumberReader(new StringReader(x));
            for (String line = r.readLine(); line != null; line = r.readLine()) {
                if (r.getLineNumber() == 1) continue;
                Constants.CONSOLE.info(line);
            }
        } catch (Exception e) {
            logger.error("print SPR status operation failed!!, exception is :{}", e.getMessage());
            new ActionException(e.getMessage(), e.getCause());
        }

	}

}
